﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using TTEntity;
using System.Data.SqlClient;
namespace TTDataAccess
{
    /// <summary>
    /// Create by ThiepND 23/3/2011
    /// </summary>
    public class clsDAImage
    {
        clsDAConnection con = new clsDAConnection();
        public DataTable GetById(int id)
        {
            try
            {
                string str = "select * from tblImage where id=@id";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@id", id);
                SqlDataAdapter da = new SqlDataAdapter(com);
                DataTable dt = new DataTable();
                da.Fill(dt);
                return dt;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public DataTable GetAll()
        {
            try
            {
                string str = "select * from tblImage";
                SqlCommand com = new SqlCommand(str, con.getCon());
                SqlDataAdapter da = new SqlDataAdapter(com);
                DataTable dt = new DataTable();
                da.Fill(dt);
                return dt;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public void Insert(clsImage obj)
        {
            try
            {
                string str = "insert into tblImage(Name,Link,TypeId)" +
                    " values(@Name,@Link,@TypeId)";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@Name", obj.Name);
                com.Parameters.AddWithValue("@Link", obj.Link);
                com.Parameters.AddWithValue("@TypeId", obj.TypeId);
                
                con.openCon();
                com.ExecuteNonQuery();
                con.closeCon();
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public void Update(clsImage obj)
        {
            try
            {
                string str = "Update tblImage set Name=@Name,Link=@Link,TypeId=@TypeId where id=@id";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@Name", obj.Name);
                com.Parameters.AddWithValue("@Link", obj.Link);
                com.Parameters.AddWithValue("@TypeId", obj.TypeId);
                con.openCon();
                com.ExecuteNonQuery();
                con.closeCon();
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public void Delete(int id)
        {
            try
            {
                string str = "delete from tblImage where id=@id";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@id", id);
                con.openCon();
                com.ExecuteNonQuery();
                con.closeCon();
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
    }
}
